//
//  JourneyViewController.m
//  YouGoWeGo
//
//  Created by LiuFei on 14/11/20.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "JourneyViewController.h"

#import "CustomJourTableCell.h"

#import "AreaModel.h"
@interface JourneyViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation JourneyViewController

- (void)dealloc
{
    [_trip_id release];
    [_jourTable release];
    [_jourArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.jourArray = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self createTable];
    [self startRequest];
    
}

- (void)startRequest
{
//
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/", self.trip_id]];
    NSURLRequest *requsest = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requsest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *arr = [dic objectForKey:@"days"];
        for (NSMutableDictionary *sDic in arr) {
            for (NSMutableDictionary *ssDic in [sDic objectForKey:@"waypoints"]) {
                
                AreaModel *model = [[AreaModel alloc] init];
                [model setValuesForKeysWithDictionary:ssDic];
                [self.jourArray addObject:model];
                
            }
        }
        
        [_jourTable reloadData];
    }];
}

#pragma mark - 创建tableview
- (void)createTable
{
    self.jourTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    
    [_jourTable setDelegate:self];
    [_jourTable setDataSource:self];
    
    [_jourTable setRowHeight:400];
    [self.view addSubview:_jourTable];
    [_jourTable release];
    
}

#pragma mark - numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_jourArray count];
}

#pragma mark - cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"jourPool";
    CustomJourTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[[CustomJourTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID] autorelease];
    }
    
    AreaModel *model = [self.jourArray objectAtIndex:indexPath.row];
    cell.model = model;
    
//    cell.contentLabel.text = @"许多的美丽总是在不经意间从我的指缝中滑落，依稀耳边响起的总是聆听不断的丁零。没法在花开花落时不伤感难过。";
//    cell.timeLabel.text = @"2012.02.23";
//    cell.regionLabel.text = @"中国,台湾";
    
    return cell;
}

#pragma mark - didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}





@end
